<%@ page language="java" contentType="text/html; charset=UTF-8"
         pageEncoding="UTF-8" %>
<%@ include file="/WEB-INF/jspf/taglibs.jspf" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<body>
<link rel="stylesheet" type="text/css" media="all" href="/js/daterangepicker/daterangepicker.css" />
<script type="text/javascript" src="http://netdna.bootstrapcdn.com/bootstrap/3.3.2/js/bootstrap.min.js"></script>
<script type="text/javascript" src="/js/daterangepicker/moment.js"></script>
<script type="text/javascript" src="/js/daterangepicker/daterangepicker.js"></script>
<link rel="stylesheet" href="/styles/sitetype.css" type="text/css">
<script type="text/javascript">
    localPath="campaign"
</script>
<div class="p-h-md p-v bg-white box-shadow pos-rlt">
    <h3 class="no-margin have-btn-title">${ form.frontCampaign.name}</h3>
    <a class="btn btn-sm btn-primary pull-right" href="/campaigns/list">返回广告计划列表</a>
    <input type="hidden" id="adCount" value="${form.adCount}">
    <input type="hidden" id="offerMessage" value="${form.offerMessage}">
    <input type="hidden" id="limitAdCount" value="${userContext.member.limitAdCount}">
    <a href="javaScript:void(0)" type="button" ui-sref="app.add" class="btn btn-sm btn-primary ng-click-active pull-right" id="add-advert"
       <c:if test="${userContext.member.operationType==1||(userContext.fromBackend ==true&&userContext.member.operationType==2)}">style="display:none"</c:if>>
        <i class="glyphicon glyphicon-plus"></i>
        新建广告
    </a>
</div>

<div class="p-md sitetype-cotnainer">
    <div class="panel no-border panel-no-margin-bottom">
        <div class="panel-heading clear add-ads-advert-btn">
            <button id="budgetEditButton" class="btn btn-sm btn-default pull-right"
                    <c:if test="${userContext.member.operationType==1&&userContext.fromBackend ==false||(userContext.fromBackend ==true&&userContext.member.operationType==2)}">style="display:none"</c:if>
            >修改预算
            </button>
            <div class="col-md-2"><h5>计划日预算：<strong id="dayBudget">
                <c:choose>
                    <c:when test="${form.frontCampaign.dayBudget !=null }">
                        ￥<fmt:formatNumber value="${form.frontCampaign.dayBudget}" maxFractionDigits="3"/>
                    </c:when>
                    <c:otherwise>
                        不限
                    </c:otherwise>
                </c:choose>
            </strong></h5></div>
        </div>
    </div>

    <%--<div class="panel no-border panel-no-margin-bottom" id="gg-no-margin-bottom"--%>
         <%--<c:if test="${userContext.member.operationType==1}">style="display:none"</c:if>>--%>
        <%--<div class="panel-heading clear add-ads-advert-btn">--%>
            <%--<button id="moneyEditButton" class="btn btn-sm btn-default pull-right "--%>
                    <%--<c:if test="${userContext.member.operationType==1&&userContext.fromBackend ==false||(userContext.fromBackend ==true&&userContext.member.operationType==2)}">style="display:none"</c:if>--%>
            <%-->修改出价</button>--%>
            <%--<div class="col-md-2"><h5>广告出价：<strong id="adOffer">--%>
                <%--<c:choose>--%>
                    <%--<c:when test="${form.frontCampaign.adOffer !=null }">--%>
                        <%--￥<fmt:formatNumber value="${form.frontCampaign.adOffer}" maxFractionDigits="3"/>--%>
                    <%--</c:when>--%>
                    <%--<c:otherwise>--%>
                        <%--不限--%>
                    <%--</c:otherwise>--%>
                <%--</c:choose>--%>
            <%--</strong></h5></div>--%>
        <%--</div>--%>
    <%--</div>--%>
    <div class="panel b-light">
    <div class="panel-heading clear">
        <div class="col-md-2 pull-right time-config">
            <input type="text" id="config-demo" class="form-control">
            <i class="glyphicon glyphicon-calendar fa fa-calendar"></i>
            <input type="hidden" name="start_date"  value="${form.start }" class="text-center form-control Wdate" id="start">
            <input type="hidden" name="end_date"  value="${form.end }" class="text-center form-control Wdate" id="end">

        </div>
        <span>总体情况</span>
    </div>
    <div id="detailDataDiv" class="panel no-border sitetype-chart-top-item">

    </div>
    <input type="hidden" id="sitesetType" name="sitesetType" value="${form.sitesetType }"/>
    <!-- 默认搜索日期  -->
    <c:if test="${userContext.member.operationType==1}">
        <input type="hidden" name="timeType"  id="timeType" value="3" />
    </c:if>
    <c:if test="${userContext.member.operationType==2}">
        <input type="hidden" name="timeType"  id="timeType" value="1" />
    </c:if>
    <input type="hidden" id="campaignId" value="${form.frontCampaign.id}">
</div>
<script type="text/javascript">
    $().ready(function () {


        $('#config-demo').daterangepicker({
            dateFormat: 'yy-mm-dd',
            "ranges": {
                "今天": [moment().startOf('day'), moment()],
                "昨天": [moment().subtract('days', 1).startOf('day'), moment().subtract('days', 1).endOf('day')],
                "过去7天": [moment().subtract('days', 6), moment()],
                "过去30天": [moment().subtract('days', 29), moment()]
            },
            opens : 'left', //日期选择框的弹出位置
            applyClass : 'btn-small btn-primary blue',
            cancelClass: 'cancel-btn-style',
            maxDate: new Date(),
            locale : {
                applyLabel : '确定',
                cancelLabel : '取消',
                customRangeLabel : '自定义',
                daysOfWeek : [ '日', '一', '二', '三', '四', '五', '六' ],
                monthNames : [ '一月', '二月', '三月', '四月', '五月', '六月',
                           '七月', '八月', '九月', '十月', '十一月', '十二月' ],
                firstDay : 1,
                format : 'YYYY-MM-DD',
            },
        }, function(start, end, label) {
            var index = '';
            
            switch(label)
            {
            case "今天":
              index = 1;
              break;
            case "昨天":
              index = 2;
              break;
            case "过去7天":
              index = 3;
              break;
            case "过去30天":
              index = 4;
              break;
            case "自定义":
              index = 5;
              break;
            }
            $('#start').val(start.format('YYYY-MM-DD'));
            $('#end').val(end.format('YYYY-MM-DD'));
            handleDashboard(start.format('YYYY-MM-DD'), end.format('YYYY-MM-DD'), index);
        });

        // 修改预算 old
        $("#budgetEditButton").click(function () {
            var dayBudget = ${ empty form.frontCampaign.dayBudget ? 0 : form.frontCampaign.dayBudget };
            bootbox.dialog({
                title: "修改预算",
                message: "<div class='well sitesetTypeModal' style='margin-top:25px;'>" +
                "<form id='forms' class='form-horizontal' role='form'>" +
                "<div class='form-group'><label class='col-sm-3 control-label no-padding-right' for='txtNewPwd1'>修改预算(元): </label><div class='col-sm-5 remark-input'>" +
                "<input  id='remark' name='dayBudget' class='text form-control' value='"+dayBudget+"'/></div><span class='control-span' style='vertical-align: -8px;'>(日预算不低于100元)</span></div>" +
                "<div class='form-group'><label class='col-sm-12 control-label no-padding-right' for='txtOldPwd'><span class='control-span' id='showMsg' style='color: red'></span></label><div class='col-sm-12'>" +
                "<div class='space-4'></div>" +
                "</form></div><p class='hint'>温馨提示：预算修改后，第二天生效。</p>",
                buttons: {
                    "success": {
                        "label": "<i class='icon-ok'></i> 保存",
                        "className": "btn-sm btn-primary",
                        "callback": function () {
                            var pattern = /^([1-9][\d]{0,7}|0)(\.[\d]{1,2})?$/
                            var re = new RegExp(pattern);
                            var txt = $("#remark").val();
                            var id = $("#campaignId").val();

                            if (!re.test(txt)||parseFloat(txt)<100) {
                                $("#showMsg").html("");
                                $("#showMsg").html("请输入正确的金额,最多输入两位小数.");
                                return false;
                            }
                            var data = {"dayBudget": txt};
                            $.ajax({
                                url: "/campaigns/edit/day_budget/" + id,
                                type: "POST",
                                dataType: "json",
                                data: data,
                                cache: false,
                                async: false,
                                success: function (data) {
                                    showMsg("success","预算修改成功！");
                                    setTimeout("location.reload()", 1000);
                                }
                            });
                        }
                    },
                    "cancel": {
                        "label": "<i class='icon-info'></i> 取消",
                        "className": "btn-sm btn-default",
                        "callback": function () {
                        }
                    }
                }
            })
        });


        // 修改出价  new
        $("#moneyEditButton").click(function () {
            var adOffer = $("#adOffer").html();
            var offerMessage = $("#offerMessage").val();
            var campaignType = ${ form.frontCampaign.campaignType};
            var pcMinAdOffer = ${userContext.member.pcMinAdOffer};
            var mobileMinAdOffer = ${userContext.member.mobileMinAdOffer};
            bootbox.dialog({
                title: "修改出价",
                message: "<div class='well ' style='margin-top:25px;'>" +
                "<form id='forms' class='form-horizontal' role='form'>" +
                "<div class='form-group'><label class='col-sm-12 control-label no-padding-right' for='txtOldPwd'>当前出价: <span class='control-span'>" + adOffer + " </span></label><div class='col-sm-12'>" +
                "<div class='space-4'></div>" +
                "<div class='form-group'><label class='col-sm-2 control-label no-padding-right' for='txtNewPwd1'>修改出价: </label><div class='col-sm-9 remark-input'>" +
                "<input  id='money-remark' name='adOffer' class='text form-control' maxlength='255' /><span>"+offerMessage+"</span></div></div>" +
                "</form></div><p class='hint'>温馨提示：出价修改后，第二天生效；若不修改，第二天的广告出价仍按照当前出价进行投放。</p>",
                buttons: {
                    "success": {
                        "label": "<i class='icon-ok'></i> 保存",
                        "className": "btn-sm btn-primary",
                        "callback": function () {
                            var pattern = /^([1-9][\d]{0,7}|0)(\.[\d]{1,2})?$/;
                            var re = new RegExp(pattern);
                            var txt = $("#money-remark").val();
                            var id = $("#campaignId").val();
                            var minAdOffer=0;
                            if(1==campaignType){
                                minAdOffer = pcMinAdOffer;
                            }else{
                                minAdOffer = mobileMinAdOffer;
                            }
                            if (!re.test(txt)||parseFloat(minAdOffer)>parseFloat(txt)) {
                                bootbox.dialog({
                                    message: "请正确输入金额,最多输入两位小数,且最低出价为"+minAdOffer,
                                    buttons: {
                                        "success": {
                                            "label": "<i class='icon-ok'></i> 确定",
                                            "className": "btn-sm btn-primary",
                                            "callback": function () {
                                            }
                                        }
                                    }
                                });
                                return false;
                            }
                            var data = {"adOffer": txt};
                            $.ajax({
                                url: "/campaigns/edit/ad_offer/" + id,
                                type: "POST",
                                dataType: "json",
                                data: data,
                                cache: false,
                                async: false,
                                success: function (data) {
                                    bootbox.dialog({
                                        message: "修改成功",
                                        buttons: {
                                            "success": {
                                                "label": "<i class='icon-ok'></i> 确定",
                                                "className": "btn-sm btn-success",
                                                "callback": function () {
                                                    location.reload();
                                                }
                                            }
                                        }
                                    });
                                }
                            });
                        }
                    },
                    "cancel": {
                        "label": "<i class='icon-info'></i> 取消",
                        "className": "btn-sm btn-default",
                        "callback": function () {
                        }
                    }
                }
            })
        });
        //判断广告创建数量是否超过限制
        $("#add-advert").click(function(){
            var adCount = parseInt($("#adCount").val());
            var limitAdCount = parseInt($("#limitAdCount").val());
            if(adCount<limitAdCount){
                window.location.href = "/ads/add?"+Math.random();
            }else{
                bootbox.dialog({
                    title: "提示",
                    message: "您创建的广告数量已超出最大限制，如需增加，请联系海量通。",
                    buttons: {
                        "success": {
                            "label": "<i class='icon-ok'></i> 确定",
                            "className": "btn-sm btn-primary",
                            "callback": function () {
                                return;
                            }
                        }
                    }
                })
            }
        })
    })

    function handleDashboard(start, end, timeType) {
        $.ajax({
            url: "/ajax/sitetype/detail/" + $("#campaignId").val(),
            data: {start: start, end: end, sitesetType: $("#sitesetType").val(), timeType: timeType},
            success: function (res) {
                $("#detailDataDiv").html(res)
            }
        })
    }
    $(function () {
        var timeType = $("#timeType").val();
        if(3==parseInt(timeType)){
            var start = formatDate(new Date(), 6);
            var end = formatDate(new Date(), 0);
            handleDashboard(start, end, timeType);
        }
        if(1==parseInt(timeType)){
            var start = formatDate(new Date(), 0);
            var end = formatDate(new Date(), 0);
            handleDashboard(start, end, timeType);
        }
    })
    var formatDate = function formatDateTime(date, count) {
        var time = date.getTime() - 1000 * 60 * 60 * 24 * count;
        var dateTime = new Date(time);
        var y = dateTime.getFullYear();  //年
        var m = dateTime.getMonth() + 1;  //月
        m = m < 10 ? ('0' + m) : m;
        var d = dateTime.getDate();  //日
        d = d < 10 ? ('0' + d) : d;
        return y + '-' + m + '-' + d;
    }

    function getImage(aid, crtSize) {
        $.ajax({
            url: "/ajax/image",
            data: {aid: aid, crtSize: crtSize},
            success: function (res) {
                bootbox.dialog({
                    size: 'large',
                    message: '<div class="panel text-center"><img src="' + res[0].imageUrl + '"  width="600" /><br/> <p>' + res[0].title + '</br></p></div>',
                    title: "广告预览",
                });
            }
        })
    }
</script>
</body>
